package operator.combine;

import java.util.ArrayList;
import java.util.concurrent.Callable;

import io.reactivex.Observable;
import io.reactivex.functions.BiConsumer;
import io.reactivex.functions.Consumer;
import util.Printer;

class CollectOperator {

    public static void main(String[] args) {
        collect();
    }

    /**
     * 收集原始 Observable 发射的所有数据到一个可变的数据结构
     */
    static void collect(){
        Observable.just("A","B","C","D")
                .collect(new Callable<ArrayList<String>>() {
                    @Override
                    public ArrayList<String> call() throws Exception {
                        return new ArrayList<String>();
                    }
                }, new BiConsumer<ArrayList<String>, String>() {
                    @Override
                    public void accept(ArrayList<String> list, String s2) throws Exception {
                        list.add(s2);
                    }
                }).subscribe(new Consumer<ArrayList<String>>() {
            @Override
            public void accept(ArrayList<String> list) throws Exception {
                Printer.print(list);
            }
        });
    }


}
